package com.markerhub.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.markerhub.common.dto.StatuDto;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.*;
import com.markerhub.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

import static io.netty.handler.codec.DateFormatter.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wang
 * @since 2022-03-18
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserRoleController extends BaseController {
    @Autowired
    SysSemesterCourseService sysSemesterCourseService;

    @Autowired
    SysCourseService sysCourseService;

    @Autowired
    SysSemesterService sysSemesterService;

    @Autowired
    SysUserCourseService sysUserCourseService;

    @PostMapping("/switch")//选课开关
    public Result save(@Validated @RequestBody StatuDto st) {
        LocalDate localDate1 = LocalDate.now();
        String year=String.valueOf(localDate1.getYear());
        Integer month=localDate1.getMonthValue();
        String currentSeason=new String();
        if(2<month && month<8){
            currentSeason=year+'春';
        }else{
            currentSeason=year+'秋';
        }

        //获取当前学期编号
        Long id=sysSemesterService.getOne(new QueryWrapper<SysSemester>().eq("season",currentSeason)).getId();
        //修改选课状态
        SysUserRole sysUserRole =sysUserRoleService.getOne(new QueryWrapper<SysUserRole>().eq("id",1));
        Long beforId=sysUserRole.getUserId();
        sysUserRole.setUserId(Long.valueOf(st.getStatu()));
        sysUserRole.setId(Long.valueOf(1));
        sysUserRoleService.updateById(sysUserRole);

        if(st.getStatu()==2){//开始踢人
            //首先获得该学期所有开的课程
            List<SysCourse> sysCourses=sysCourseService.list(new QueryWrapper<SysCourse>().eq("statu",0));
            for(SysCourse i:sysCourses){
                //查询该课程的开课学期
                List<SysSemesterCourse> sysSemesterCourses=sysSemesterCourseService.list(new QueryWrapper<SysSemesterCourse>().eq("course_id",i.getId()));
                List<Long> seasonns=new ArrayList<>();
                for(SysSemesterCourse j:sysSemesterCourses){
                    seasonns.add(j.getSemesterId());
                }
                if(!seasonns.contains(id)){
                    sysCourses.remove(i);
                }
            }
            //判断选课容量和已选人数
            for(SysCourse i:sysCourses){
                if(i.getCurrentNum()>i.getVolume()){
                    //踢人
                    //升序排列
                    QueryWrapper<SysUserCourse> queryWrapper = new QueryWrapper<>();

                    Map<String, Object> queryParamsMap = new HashMap<>();
                    queryParamsMap.put("course_id",i.getId());
                    queryParamsMap.put("isPassed", 0);
                    queryParamsMap.put("semester", currentSeason);

                    queryWrapper.allEq(queryParamsMap);
                    List<SysUserCourse> sysUserCourses=sysUserCourseService.list(queryWrapper.orderByAsc("stu_username"));

                    for(int j=0;j<sysUserCourses.size();j++){
                        if(j>i.getVolume()-1){
                            sysUserCourseService.removeById(sysUserCourses.get(j).getId());
                            i.setCurrentNum(i.getCurrentNum() -1);
                            sysCourseService.updateById(i);
                        }
                    }

                }
            }
        }
        return Result.succ("");
    }

    @GetMapping("/switch/get")//
    public Result getSwitch() {

        SysUserRole sysUserRole = sysUserRoleService.getObject();

        return Result.succ(sysUserRole.getUserId());


    }
}